var yjfk_app = function() {//企业管理
	var path =Constants.amUrlPath;
	var ractive;
	var yjfkApp ={
		URL:{
			list: function(){//分页查询路径post
				return path + "yjfk/getYjfkxxList";
			},
			ids: function(){//查询所有的意见反馈的id路径get
				return path + "yjfk/getYjfkxxIds";
			},
			look: function(id,wtzt){//静态页跳转路径
				return "yjfkgl-ck.html?id=" + id +"&wtzt=" + wtzt;
			},
			_delete: function(id){//删除操作路径get
				return path + "yjfk/deleteYjfk?id="+id ;
			},
			excel: function(ids){//excel导出路径get
				return path + "yjfk/pldcYjfkxx?ids="+ids ;
			},
			excelpost: function(){//excel导出路径get
				return path + "yjfk/pldcYjfkxx";
			},
			selectbox: function(codeClass){
				return path + "common/getCodeListByCodeClass?codeClass=" + codeClass;
			}
		},
		getSelectedZtVal: function(codeClass,eleId){
	        $.ajax({
				type: "get",
				url: yjfkApp.URL.selectbox(codeClass),				
				async: false,
				success: function(data) {//数据绑定
					data.unshift({"code": -1, "codedesc": "All"});
	                store.set(eleId, JSON.stringify(data));
	                selecttextInfo();
	                setWDCode("code_"+eleId, "-1");
				}
    	    })
    	    return $('#'+eleId).val(); 
		},
		excelPldc: function(ids){//excel批量导出get方式
			window.open(yjfkApp.URL.excel(ids));
		},
		openPostWindow: function(url,ids){//excel批量导出post方式
			var excelForm = document.createElement("form");
			excelForm.id = "excelForm1";
		    excelForm.method = "post";
		    excelForm.action = url;
		    var hideInput1 = document.createElement("input");
		    hideInput1.type = "hidden";
		    hideInput1.name="ids"; //后台要接受这个参数来取值
		    hideInput1.value = ids; //后台实际取到的值
		    excelForm.appendChild(hideInput1);
		    document.body.appendChild(excelForm);
		    
		    excelForm.submit();
		    document.body.removeChild(excelForm);
		},
		_delete: function(id){
			$.get(yjfkApp.URL._delete(id),function(data){
				if(!data.result){
					alert({txt: "Failed to delete!",callBackFun: callBackSuccessFun});					
					function callBackSuccessFun(){}
					return;
				}	
				//成功提示：
				var objs = {txt:"Delete successful!",callBackFun:fn}
				success(objs);
				function fn() {window.location.reload()}
			});
		},
		getYjfkIds: function(objs){//获取所有ids
			var result = "";
			$.ajax({
				type: "post",
				url: yjfkApp.URL.ids(),				
				async: false,
				data: JSON.stringify(objs),
				contentType: "application/json",
				success: function(data) {
					result = data.join(',');
				}
    	    })
    	    return result; 
		}
	}
	
	//页面渲染
	var init_param = {"currPage" : 1,"pageSize" : Constants.page.pagesize};
	function renderView() {//渲染页面
		ractive = new Ractive({
			el: ".main-contexts",
			template: "#yjfkgl-template",
			oncomplete: function(){
				init_param.wtzt = yjfkApp.getSelectedZtVal('WTZT','WTZT');//加载状态下拉列表          		
				initTable(init_param);//初始化表格
			}
		});
		ractive.on({
			search: function() {//查询
				init_param.wtzt = $("#WTZT").val();
				var searchParam = {
					bt: ractive.get("bt") ? $.trim(ractive.get("bt")) : "",
					startTcsj: ractive.get("startTcsj"),
					endTcsj: ractive.get("endTcsj")
				};
				ractive.set("bt",(ractive.get("bt") ? $.trim(ractive.get("bt")) : ""));//去空格
				var objs = $.extend(init_param, searchParam);//合并参数
				initTable(objs);//查询后加载表格数据
			},
			pldc: function(){//excel批量导出选中的记录
				manager.isLogin();
				//var ids = $("#wd-table").getAllCheckedId();
				/*if(!ids){//提示
					alert({txt: "请勾选需要导出的反馈记录！",callBackFun: callBackSuccessFun});					
					function callBackSuccessFun(){}
					return false;
				}*/
				//根据查询条件导出数据库数据记录：
				init_param.wtzt = $("#WTZT").val();
				var searchParam = {
					bt: $.trim(ractive.get("bt")),
					startTcsj: ractive.get("startTcsj"),
					endTcsj: ractive.get("endTcsj")
				};
				var objs = $.extend(init_param, searchParam);//合并参数
				var ids = yjfkApp.getYjfkIds(objs);
				if($.trim(ids)==''){
					alert({txt: "No data!",callBackFun: callBackSuccessFun});					
					function callBackSuccessFun(){}
				}else{//excel批量导出
					yjfkApp.openPostWindow(yjfkApp.URL.excelpost(),ids)//post请求
					//yjfkApp.excelPldc(ids); //get请求
				}
				
			}
		});
	}
	//加载表格数据
	function initTable(param) {
		var _table_title =  ['Name', 'Comment Title', 'Classification of comment','Present time', 'Status', 'Replier', 'Operate'];
		var _col_model = [
	        {name: 'tcrmc',width: 100,float: 'left'},
			{name: 'bt',width: 180,float: 'left'},
			{name: 'wtlxDesc',width: 80},
			{name: 'tcsj',width: 160,
				innerhtml: function(opts){
					return   moment(opts.tcsj).format("YYYY-MM-DD HH:mm:ss")
				}
			},
			{name: 'wtztDesc',width: 60},
			{name: 'fkrmc',width: 100,float: 'left'},
			{name: 'cz',width: 200,
				innerhtml: function(opts) {				
					var str = '';
					if(opts.wtzt == Constants.code.WTZT.WTZT_YCL){
						str += '<span class="ad-gridlist-btn" onclick="yjfk_app.look(\'' + opts.id +'\',\''+ opts.wtzt + '\')"  />View</span>';	
					}
					if(opts.wtzt == Constants.code.WTZT.WTZT_WCL){
						str += '<span class="ad-gridlist-btn" onclick="yjfk_app.answer(\'' + opts.id  +'\',\''+ opts.wtzt + '\')" />Reply</span>';	
					}
					str += '<span class="ad-gridlist-btn btn2"  onclick="yjfk_app.deleteFk(\'' + opts.id + '\')"  />Delete</span>';
						
					return str;
				}
			}
		];
		jQuery("#wd-table").wdGrid({
			url: yjfkApp.URL.list(),
			colNames: _table_title,
			colModel: _col_model,
			dataType: "GET",
			submitType: "noForm",
			postData: param,
			//checkflg: true,
			pager: "#wd-pager",
			rowList: Constants.page.rowList,
			pagesize: Constants.page.pagesize
		});
	}
	
	function look(id,wtzt) {//查看操作
		location.href =  yjfkApp.URL.look(id,wtzt);
	}
	function answer(id,wtzt) {//回复操作
		location.href =  yjfkApp.URL.look(id,wtzt);
	}
	function deleteFk(id) {//删除操作
		confirm({
			txt: "Confirm delete?",
			successCallBackFun: successCallBackFun,
			cancelCallBackFun: cancelCallBackFun
		});	
		function successCallBackFun(){yjfkApp._delete(id)}//确定删除
		function cancelCallBackFun(){}//取消删除
	}
	
	return {
		init: function() {
			renderView();
		},
		look: function(id,wtzt){
			look(id,wtzt);
		},
		answer: function(id,wtzt){
			answer(id,wtzt);
		},
		deleteFk: function(id){
			deleteFk(id);
		}
	};
}();
$(document).ready(yjfk_app.init);
